package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.RecordGroup;
import com.shaw.workrecord.entity.PageRequest;
import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.RecordGroupMapper;
import com.shaw.workrecord.pojo.request.RecordGroupAddRequest;
import com.shaw.workrecord.pojo.request.RecordGroupEditRequest;
import com.shaw.workrecord.pojo.response.RecordGroupResponse;
import com.shaw.workrecord.service.IRecordGroupService;
import com.shaw.workrecord.utils.JWTUtils;
import com.shaw.workrecord.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecordGroupServiceImpl extends ServiceImpl<RecordGroupMapper, RecordGroup> implements IRecordGroupService {

    @Autowired
    private RecordGroupMapper mapper;

    @Override
    public PageResponse<RecordGroupResponse> getRecordGroupPage(PageRequest pageRequest) {
        LambdaQueryWrapper<RecordGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(RecordGroup::getUserId, JWTUtils.getUserId());
        Page<RecordGroup> page = new Page<>(pageRequest.getPageIndex(), pageRequest.getPageSize());

        Page<RecordGroup> result = mapper.selectPage(page, wrapper);

        return PageUtils.toPageResponse(result, RecordGroupResponse.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRecordGroup(RecordGroupAddRequest request) {
        RecordGroup recordGroup = BeanUtil.copyProperties(request, RecordGroup.class);
        recordGroup.setUserId(JWTUtils.getUserId());
        return save(recordGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editRecordGroup(RecordGroupEditRequest request) {
        RecordGroup recordGroup = BeanUtil.copyProperties(request, RecordGroup.class);
        recordGroup.setUserId(JWTUtils.getUserId());
        int result = mapper.updateById(recordGroup);
        return result > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRecordGroup(Integer id) {
        LambdaQueryWrapper<RecordGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(RecordGroup::getId).eq(RecordGroup::getId, id).eq(RecordGroup::getUserId, JWTUtils.getUserId());
        RecordGroup recordGroup = mapper.selectOne(wrapper);
        if (recordGroup == null) {
            throw new CustomerException("不存在记录组");
        }
        int result = mapper.deleteById(id);
        return result > 0;
    }
}
